
#include "testMysqlInstance.h"


CPPUNIT_TEST_SUITE_REGISTRATION (testMysqlObject);

void testMysqlObject :: setUp (void){
}

void testMysqlObject :: tearDown (void) {
}

void testMysqlObject :: testMakeInstance (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	delete sql;
}


void testMysqlObject :: testConnectWithoutLoginReturnsError (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		false,
		sql->connect("localhost","nobody","nopasswd","nodatabase",UNITTEST_MYSQL_PORT,"")
	);
	delete sql;
}

void testMysqlObject :: testConnectWithLoginReturnsTrue (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);
	delete sql;
}


void testMysqlObject :: testGetDatabaseReturnsTestString (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);
	CPPUNIT_ASSERT_EQUAL (
		(string)"test",
		sql->getDatabase()
	);
	delete sql;
}

void testMysqlObject :: testQueryWithWrongSelectReturnError (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);
	CPPUNIT_ASSERT_EQUAL (
		-1,
		sql->query("SELECT * NOTHING test")
	);
	delete sql;
}

void testMysqlObject :: testQueryWithSelectReturnsNothing (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);
	sql->query(sql->loadFromFile("res/testcase.sql"));
	CPPUNIT_ASSERT_EQUAL (
		0,
		sql->query("SELECT * FROM testcase1")
	);
	sql->query("DROP TABLE testcase1");
	delete sql;
}

///////////////////////////////////////////////////////////////////

/**
 * Add records to the database "test" on localhost
 */

void testMysqlObject :: testSetupTableStrukturForTestcase (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);
	sql->query(sql->loadFromFile("res/testcase.sql"));

	sql->query("INSERT INTO `testcase1` VALUES(1,\"String1\",12.34,\"Blob Wert1\",\"Textfile here1\");");
	sql->query("INSERT INTO `testcase1` VALUES(2,\"String2\",23.45,\"Blob Wert2\",\"Textfile here2\");");
	sql->query("INSERT INTO `testcase1` VALUES(3,\"String3\",34.56,\"Blob Wert3\",\"Textfile here3\");");

	delete sql;
}
void testMysqlObject :: testEraseTableStrukturForTestcase (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);
	sql->query("DROP TABLE testcase1");
	delete sql;
}

///////////////////////////////////////////////////////////////////

/**
 * Start testcases for mysql Object with records in database
 *
 * Configure the database with the settings
 *
 * host: localhost
 * port: 5801
 * user: test
 * pass:
 * database: test
 */

///////////////////////////////////////////////////////////////////

void testMysqlObject :: testMysqlQueryReturnsCorrectCount (void) {
	CPPUNIT_ASSERT_EQUAL (
		true,
		mysql_connect("localhost","root","","test",UNITTEST_MYSQL_PORT)
	);
	int result = mysql_query("SELECT * FROM testcase1");
	CPPUNIT_ASSERT_EQUAL (
		3,
		result
	);
	mysql_close();
}

void testMysqlObject :: testSelectTableReturnsCorrentFields (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);

	array data = sql->getFieldsFromTable("testcase1");

	CPPUNIT_ASSERT_EQUAL (
		(string)"test1",
		data.key()
	);
	CPPUNIT_ASSERT_EQUAL (
		(string)"PRI",
		data["test1"].get()
	);
	data.next();
	CPPUNIT_ASSERT_EQUAL (
		(string)"test2",
		data.key()
	);
	data.next();
	CPPUNIT_ASSERT_EQUAL (
		(string)"test3",
		data.key()
	);

	mysql_close();

}

void testMysqlObject :: testQueryWithSelectReturnsCorrectRecords (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);

	CPPUNIT_ASSERT_EQUAL (
		3,
		sql->query("SELECT * FROM testcase1")
	);
	delete sql;
}

void testMysqlObject :: testQueryWithDeleteOneRecordAndSelectReturnsCorrectRecords (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);

	CPPUNIT_ASSERT_EQUAL (
		1,
		sql->query("DELETE FROM testcase1 WHERE test2='String3'")
	);
	CPPUNIT_ASSERT_EQUAL (
		2,
		sql->query("SELECT * FROM testcase1")
	);
	delete sql;
}


void testMysqlObject :: testQueryWithUpdateOneRecordReturnsCorrectString (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);
/*
	CPPUNIT_ASSERT_EQUAL (
		1,
		sql->query("UPDATE testcase1 SET test3='100.89' WHERE test2='String3'")
	);
	CPPUNIT_ASSERT_EQUAL (
		2,
		sql->query("SELECT * FROM testcase1")
	);
*/
	delete sql;
}
void testMysqlObject :: testMysqlFetchAssocReturnsCorrectRecords (void) {
	mysqlInstance *sql = mysqlInstance::makeInstance("");
	CPPUNIT_ASSERT_EQUAL (
		true,
		sql->connect("localhost","test","","test",UNITTEST_MYSQL_PORT,"")
	);

	CPPUNIT_ASSERT_EQUAL (
		2,
		sql->query("SELECT * FROM testcase1 ORDER BY test3 DESC")
	);

	object *obj = sql->mysqlFetchAssoc();
	CPPUNIT_ASSERT_EQUAL (
		(string)"String2",
		obj->get("test2")
	);
	CPPUNIT_ASSERT_EQUAL (
		(string)"23.45",
		obj->get("test3")
	);
	CPPUNIT_ASSERT_EQUAL (
		(string)"Blob Wert2",
		obj->get("test4")
	);
	CPPUNIT_ASSERT_EQUAL (
		(string)"Textfile here2",
		obj->get("test5")
	);

	obj = sql->mysqlFetchAssoc();

	CPPUNIT_ASSERT_EQUAL (
		(string)"String1",
		obj->get("test2")
	);
	CPPUNIT_ASSERT_EQUAL (
		(string)"12.34",
		obj->get("test3")
	);
	CPPUNIT_ASSERT_EQUAL (
		(string)"Blob Wert1",
		obj->get("test4")
	);
	CPPUNIT_ASSERT_EQUAL (
		(string)"Textfile here1",
		obj->get("test5")
	);

	delete sql;
}
